import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes=[
  {
    path: '/live',
    name: 'live',
    meta:{
      title:'直播模块',
      icon:'el-icon-s-grid'
    },
    component: ()=>import('@/views/empty.vue'),
    redirect:'/live/live-list',
    children:[
      {
        path: '/live/live-list',
        name: 'live-list',
        meta:{
          title:'直播列表',
          icon:'el-icon-s-grid'
        },
        component: ()=>import('@/views/live/live-list/index.vue')
      },
    ]
  },
  {
    path: '/distribute',
    name: 'distribute',
    meta:{
      title:'分销模块',
      icon:'el-icon-s-grid'
    },
    component: ()=>import('@/views/empty.vue'),
    redirect:'/distribute/c',
    children:[
      {
        path: '/distribute/c',
        name: 'c',
        meta:{
          title:'c端裂变',
          icon:'el-icon-s-grid'
        },
        component: ()=>import('@/views/distribute/c_client/index.vue')
      },
    ]
  }
]


// 重写push|replace方法
//先把VueRouter的push和replace方法保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location, resolve, reject) {
    // 此函数上下文(this指向)为VueRouter的一个实例
    if (resolve && reject) {    //如果我们自己指定了成功/失败的回调，则自己传入
        originPush.call(this, location, resolve, reject)
        //若此时直接使用originPush()，相当于调用了函数originPush()，则函数内的this指向window（内部代码将无法执行）。故应用call或apply方法修改this指向
    } else {    //如果我们没有指定成功/失败的回调，则自动帮我们生成，防止报错
        originPush.call(this, location, () => { }, () => { })
    }
}

// replace方法同理
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
      originReplace.call(this, location, resolve, reject)
  } else {
      originReplace.call(this, location, () => { }, () => { })
  }
}
const router = new VueRouter({
  mode: 'history',
  base: import.meta.env.BASE_URL,
  routes:[
    {
      path: '/',
      name: 'home',
      component: ()=>import('@/views/empty.vue'),
      children:routes
    },
  ]
})


export {routes};
export default router
